# author:lustime
# time:2017.11.22

from urllib.request import urlopen
import os
from bs4 import BeautifulSoup
from Tools.scripts.treesync import raw_input


def urlBS(url):
    response = urlopen(url)
    html = response.read()
    soup = BeautifulSoup(html)
    return soup


def main(url):
    soup = urlBS(url)
    link = soup.select('.booklist a')
    path = os.getcwd() + '/读者文章/'
    if not os.path.isdir(path):
        os.mkdir(path)
    for item in link:
        newurl = baseurl + item['href']
        result = urlBS(newurl)
        title = result.find('h1').string
        writer = result.find(id='pub_date').string.strip()
        filename = path + title + '.txt'
        print(filename)
        new = open(filename, 'w')
        new.write('<<' + title + '>>\n\n')
        new.write(writer + '\n\n')
        text = result.select('.blkContainerSblkCon p')
        for p in text:
            context = p.text
            new.write(context)
        new.close()


if __name__ == '__main__':
    time = raw_input("请输入需要下载的期数：\n")
    baseurl = 'http://www.52duzhe.com/' + time.rstrip() + '/'
    firsturl = baseurl + 'index.html'
    main(firsturl)
